<?php
include_once (_CORE_PATH . "Lib/HTMLPurifierLib/HTMLPurifier.auto.php");
class Mozg_Lib_Purifier implements Mozg_Lib
{
	private $_config = array();

	/**
		настройки для текста статей и разделов
	*/
	private function getConfig($type)
	{
		if (isset($this->_config[$type])) return $this->_config[$type];

		$config = HTMLPurifier_Config::createDefault();
		$config->set('Core.Encoding', 'UTF-8');
		//$config->set('Core.EscapeInvalidTags', true);
		//$config->set('Cache.SerializerPath', get_config('dataroot') . 'htmlpurifier');

		switch ($type)
		{
			case 'comment':
					$config->set('HTML.Allowed', 'a[href|title],em,p,blockquote,ul,ol,li,img[src|height|width|title|alt],sup,s,strike,b,strong,u');
					$config->set('HTML.MaxImgLength', '500');
					$config->set('AutoFormat.AutoParagraph', true);
					$config->set('AutoFormat.Linkify', true);
					$config->set('AutoFormat.RemoveEmpty', true);
				break;

			case 'html':
					$config->set('Core.ConvertDocumentToFragment', true);
					$config->set('HTML.Trusted', true);
					//$config->set('HTML.Proprietary', true);
					//$config->set('HTML.SafeObject', true);
					$config->set('HTML.SafeEmbed', true);
					//$config->set('Output.FlashCompat', true);
					$config->set('HTML.FlashAllowFullScreen', true);
					$config->set('Attr.EnableID', true);
					//$config->set('HTML.Doctype', 'HTML 4.01 Strict');//HTML 4.01 Strict //HTML 4.01 Transitional
					//$config->set('HTML', 'MaxImgLength', '600');
				break;

			default: throw new Mozg_Exception_System(Mozg_Lang::_('INVALID_CONFIG_TYPE'));
		}

		return $this->_config[$type] = $config;
	}

	public function purifyHTML($html)
	{
		return HTMLPurifier::getInstance()->purify($html, $this->getConfig('html'));
		//return $html;
	}

	public function purifyComment($html)
	{
		return HTMLPurifier::getInstance()->purify($html, $this->getConfig('comment'));
	}
}
